#include <exception>
#include <iostream>
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/config.hpp>
#include <boost/program_options/environment_iterator.hpp>
#include <boost/program_options/eof_iterator.hpp>
#include <boost/program_options/errors.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/value_semantic.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/version.hpp>
#include <boost/asio.hpp>

#include "FtServer.hpp"

namespace po = boost::program_options;

int
main(int argc, char *argv[])
{
  try
    {
      boost::asio::io_service ios;
      po::options_description desc("Allowed options");
      desc.add_options()
	("help,h", "help message")
	("port,p", po::value<int>()->default_value(12345), "port")
	("storage,s", po::value<std::string>(), "path to storage")
	;

      po::variables_map vm;
      po::store(po::parse_command_line(argc, argv, desc), vm);
      po::notify(vm);

      if (vm.count("help"))
	{
	  std::cout << desc << "\n";
	  return 1;
	}

      if (!vm.count("storage"))
	{
	  std::cout << "Path to storage not set." << std::endl;
	  throw std::exception();
	}

      FtServer srv(vm["port"].as<int>(), vm["storage"].as<std::string>(), ios);
      srv.start();
      ios.run();
    }
  catch (std::exception e)
    {
      std::cerr << "Fatal error." << std::endl;
      return 1;
    }

  return 0;
}
